package com.gary.mongo.driver;

import com.mongodb.*;
import com.mongodb.MongoClient;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import lombok.extern.slf4j.Slf4j;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author peng_rui
 * @date 2022/6/2
 */
public class MongoDriver {

    private Map<String, MongoClient> clientMap = new HashMap<>();

    public Map<String, MongoClient> getClientMap() {
        return this.clientMap;
    }

    public void fillClientMap() {
        //创建连接
        System.out.println("连接MyAliMongo...");
        MongoClient mongoClient = new MongoClient(Arrays.asList(
                new ServerAddress("120.55.99.182", 27017),
                new ServerAddress("120.55.99.182", 27018),
                new ServerAddress("120.55.99.182", 27019)),
                MongoCredential.createCredential("myroot", "articledb", "123456".toCharArray())
                , MongoClientOptions.builder().build());
        clientMap.put("myAli", mongoClient);
        System.out.println("连接HcTestMongo...");
        MongoClientOptions.builder().build();
        MongoClient mongoClient2 = new MongoClient(Arrays.asList(
                new ServerAddress("192.168.111.171", 27017),
                new ServerAddress("192.168.111.172", 27017),
                new ServerAddress("192.168.111.173", 27017)),
                MongoCredential.createCredential("sqldubbo", "hc_user_center", "mongo_sqldubbo".toCharArray())
                , MongoClientOptions.builder().build());
        clientMap.put("hcTest", mongoClient2);
    }

    public static void main(String[] args) {
        MongoDriver mongoDriver = new MongoDriver();
        mongoDriver.fillClientMap();
        Map<String, MongoClient> clientMap = mongoDriver.getClientMap();
        MongoClient mongoClient = clientMap.get("myAli");

//        //创建连接
//        System.out.println("连接mongo...");
//        MongoClient mongoClient = new MongoClient(Arrays.asList(
//                new ServerAddress("120.55.99.182", 27017),
//                new ServerAddress("120.55.99.182", 27018),
//                new ServerAddress("120.55.99.182", 27019)
//        ));
        //连接数据库
        System.out.println("连接数据库....");
        MongoDatabase database = mongoClient.getDatabase("articledb");

        //获取数据集合（表）
        System.out.println("获取集合...");
        MongoCollection<Document> comment = database.getCollection("comment");
        System.out.println("mongo连接成功");
        //查询
        System.out.println("查询...");
        FindIterable<Document> documents = comment.find();
        for (Document next : documents) {
            System.out.println(next);
        }

        //删除
//        Bson bson = Filters.eq("title", "title");
//        comment.deleteMany(bson);
//        System.out.println("查询成功...");
//        //新增
//        System.out.println("新增...");
//        Document document = new Document();
//        document.put("articleid", "100001");
//        document.put("content", "刚刚好");
//        comment.insertOne(document);
//        System.out.println("新增成功");
    }
}
